package edu.bit.water.controller;

import edu.bit.water.dao.RolesDao;
import edu.bit.water.entity.Roles;
import edu.bit.water.entity.UserEntity;
import edu.bit.water.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这里对应数据库中roles表的controller
 * 对应角色表
 * @Author： PH
 */
@Controller
@RequestMapping("/roles")
public class RolesController extends BaseController{

    @Autowired
    private RolesService RolesService = new RolesService();

    @ResponseBody
    @RequestMapping(value = "/select", method=RequestMethod.GET)
    public Map<String, Object>selectRoles() {
        Map<String, Object> result = new HashMap<>();
        UserEntity user = (UserEntity) session.getAttribute("user");
        List<Roles> list = RolesService.selectRoles();
        result.put("rows", list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/select-by-company-id", method=RequestMethod.GET)
    public Map<String, Object>selectRolesByCompanyId(String companyId) {
        Map<String, Object> result = new HashMap<>();
        List<Roles> list = RolesService.selectRolesByComId(companyId);
        result.put("rows", list);
        return result;
    }

    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, Object> insertRoles(Roles Roles) {
        Roles.changeTime();
        return RolesService.insertRoles(Roles);
    }

    /**
     * 删除角色
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, Object> deleteRoles(@RequestBody List<String>Roles) {
        return RolesService.deleteRoles(Roles);
    }


    @ResponseBody
    @RequestMapping("/update")
    public Map<String, Object> updateMetaStandard(Roles Roles) {
        Roles.changeTime();
        Map<String, Object> result = RolesService.updateRoles(Roles);
        return result;
    }

    @ResponseBody
    @RequestMapping("/deleteUserRoleRel")
    public Map<String ,Object> deleteUserRoleRel(@RequestBody List<String> listname){
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("status", "success");
        int flag=-1;
        int tmp =0;
        try {
            for(String username : listname) {
                UserEntity user = userDao.findByName(username);
                int id = user.getId();
                tmp = RolesService.deleteUserRoleRel(id);
            }
            if(tmp!=0)
                flag=1;
        } catch (Exception e) {
            e.printStackTrace();
            flag=0;
        }
        if (flag == 1) {
            modelMap.put("status", "success");
        } else {
            modelMap.put("status", "failed");
        }
        return modelMap;
    }
}